package kr.or.kead.ui.insert_update;

import javax.swing.JPanel;

import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.SwingConstants;

import kr.or.kead.domain.Depart;
import kr.or.kead.domain.Professor;
import kr.or.kead.module.EmailCheck;
import kr.or.kead.service.DaoProfessor;
import kr.or.kead.service.Daodepart;

public class ProfessorUpdate extends AbsInsertUpdate{
	

	private JLabel lblNewLabel;
	private JLabel txtCode;
	private JLabel lblNewLabel_1;
	private JTextField txtName;
	private JLabel lblNewLabel_2;
	private JComboBox<Object> comboDepart;
	private JLabel lblNewLabel_3;
	private JTextField txtCourse;
	private Daodepart daoDepart;
	private JLabel lblNewLabel_4;
	private EmailCheck txtEmail;
	
	public ProfessorUpdate(Object obj) {
		super(obj, "교수관리");
	}
	
	@Override
	protected void initDao() {
		daoDepart = new Daodepart();
		daotable = new DaoProfessor();
	}

	@Override
	protected boolean isValidCheck() {
		if(txtCode.getText().equals("")||txtName.getText().equals("")||txtCourse.getText().equals("")){
			JOptionPane.showMessageDialog(null, "비어있는 란이 있습니다!");
			txtCode.requestFocus();
			return false;
		}
		return true;
	}

	@Override       
	protected JPanel getMainPanel() {
		JPanel cp = new JPanel();
		cp.setLayout(new GridLayout(5, 2, 0, 0));
		
		lblNewLabel = new JLabel("교수코드 :");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		cp.add(lblNewLabel);
		
		txtCode = new JLabel(String.valueOf(((DaoProfessor) daotable).selectMaxIdx()+1));
		cp.add(txtCode);
		
		lblNewLabel_1 = new JLabel("교수명 :");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
		cp.add(lblNewLabel_1);
		
		txtName = new JTextField();
		cp.add(txtName);
		txtName.setColumns(10);
		
		lblNewLabel_2 = new JLabel("학과명 :");
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
		cp.add(lblNewLabel_2);
		
		ArrayList<String> arList = daoDepart.selectTableAllList();
		Object[] objList = arList.toArray();
		comboDepart = new JComboBox<>(objList);
		cp.add(comboDepart);
		
		lblNewLabel_3 = new JLabel("과목명 :");
		lblNewLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
		cp.add(lblNewLabel_3);
		
		txtCourse = new JTextField();
		cp.add(txtCourse);
		txtCourse.setColumns(10);
		
		lblNewLabel_4 = new JLabel("E-MAIL :");
		lblNewLabel_4.setHorizontalAlignment(SwingConstants.CENTER);
		cp.add(lblNewLabel_4);
		
		txtEmail = new EmailCheck();
		cp.add(txtEmail);
		return cp;
	}

	@Override
	protected Object getObject() {
		if (obj==null){// 추가
			Professor professor = new Professor(
					Integer.parseInt(txtCode.getText()),
					txtName.getText(), 
					daoDepart.selectCodeDepartByName((String)comboDepart.getSelectedItem()), 
					txtCourse.getText(),txtEmail.getEmail());
			return professor;
		}else{//수정
			Professor professor = (Professor) obj;
			professor.setCode(Integer.parseInt(txtCode.getText()));
			professor.setName(txtName.getText());
			professor.setDepartCode(daoDepart.selectCodeDepartByName((String)comboDepart.getSelectedItem()));
			professor.setCourse(txtCourse.getText());
			professor.setEmail(txtEmail.getEmail());
			return professor;
		}
	}

	@Override
	protected void updateInit() {
		Professor professor = (Professor) obj;
		txtCode.setText(professor.getCode()+"");
		txtName.setText(professor.getName());
		Depart depart = (Depart) daoDepart.selectTableById(professor.getDepartCode());
		comboDepart.setSelectedItem(depart.getName());
		txtCourse.setText(professor.getCourse());
		txtEmail.setEmail(professor.getEmail());
	}

}
